package com.eqdd.yiqidian.ui.yiqichuang;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RecyclerViewCustom;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.ChuangwenMenuListBean;
import com.eqdd.yiqidian.bean.rebuild.WriteBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditVEnterBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.ui.WriteActivity;
import com.eqdd.yiqidian.ui.base.SlimBaseMultiEditActivity;
import com.eqdd.yiqidian.ui.select.SelectFourDeepMultiTagActivity;
import com.eqdd.yiqidian.utils.SplitUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;

/**
 * @author吕志豪 .
 * @date 18-1-25  下午3:56.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class CreateChuangwenActivity extends SlimBaseMultiEditActivity {


    private RecyclerViewCustom dataBinding;
    private Bundle bundle;
    private String matchedTrade;
    private ChuangwenMenuListBean menuListBean;
    private WriteBean writeBean;


    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_recyclerview);
        initTopTitleBar(View.VISIBLE, "写创文");
        initTopRightText("发布", v -> {
            if (checkParams()) {
                OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.CREATE_CHUANG_WEN)
                        .params("userGuid", AppConstent.user.getGuid())
                        .params("title", writeBean.getTitle())
                        .params("matchedTrade", matchedTrade)
                        .params("splendidContent", setParam(1))
                        .params("ArticleContent", writeBean.getHtml())
                        .params("categoryId", menuListBean.getId())
                        .params("categoryName", menuListBean.getCategoryName())
                        .params("picUrl", writeBean.getImgUrl())
                        .params("source", 0)
                        .execute(new DialogCallBack<HttpResult>(CreateChuangwenActivity.this) {
                            @Override
                            public void onSuccess(Response<HttpResult> response) {
                                HttpResult httpResult = response.body();
                                ToastUtil.showShort(httpResult.getMsg());
                                if (httpResult.getStatus() == 200) {
                                    isSave = false;
                                    clearData();
                                    setResult(Config.SUCCESS);
                                    finish();
                                }
                            }
                        });
            } else {
                ToastUtil.showShort(msg);
            }
        });

    }

    @Override
    protected RecyclerView initRecyclerView() {
        return dataBinding.recyclerView;
    }

    @Override
    protected void setEditBeans() {
        isSave = false;
        isRead = false;
        menuListBean = new ChuangwenMenuListBean();
        menuListBean.setId(0);
        menuListBean.setCategoryName("");
        datas.add(new SlimEditVEnterBean("*写创文", "", 0, true, "请输入").setNeed(true));
        datas.add(new SlimEditVEnterBean("*创文亮点", "", 1, "请输入").setNeed(true));
        datas.add(new SlimEditVEnterBean("*创文类别", "", 2, true, "请选择").setNeed(true));
        datas.add(new SlimEditVEnterBean("创文所在文件夹", "", 3, true, "默认文件夹"));
    }

    @Override
    public void itemClick(int p) {
        switch (slimAdapterEx.getDataItem(p).getPosition()) {
            case 0:
                bundle = new Bundle();
                bundle.putSerializable(Config.BEAN, writeBean);
                startActivityForResult(WriteActivity.class, bundle, Config.WRITE);
                break;
            case 2:
                bundle = new Bundle();
                bundle.putInt(Config.OPTION, 47);
                bundle.putInt(Config.MAX_NUM, 3);
                startActivityForResult(SelectFourDeepMultiTagActivity.class, bundle, Config.SELECT_FOUR_DEEP_MULTI_TAG);
                break;
            case 3:
                bundle = new Bundle();
                bundle.putBoolean(Config.IS_SELECT, true);
                startActivityForResult(ChuangwenMenuListActivity.class, bundle, Config.CHUANGWEN_MENU_LIST);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Config.SUCCESS) {
            if (requestCode == Config.SELECT_FOUR_DEEP_MULTI_TAG) {
                ArrayList<String> types = data.getStringArrayListExtra(Config.STRING_LIST);
                matchedTrade = SplitUtil.combin(types, ";");
                slimAdapterEx.getDataItem(2).setContent(matchedTrade);
                slimAdapterEx.notifyItemChanged(2);
            } else if (requestCode == Config.CHUANGWEN_MENU_LIST) {
                menuListBean = (ChuangwenMenuListBean) data.getSerializableExtra(Config.BEAN);
                slimAdapterEx.getDataItem(3).setContent(menuListBean.getCategoryName());
                slimAdapterEx.notifyItemChanged(3);
            } else if (requestCode == Config.WRITE) {
                writeBean = (WriteBean) data.getSerializableExtra(Config.BEAN);
                slimAdapterEx.getDataItem(0).setContent(writeBean.getTitle());
                slimAdapterEx.notifyItemChanged(0);
            }
        }
        super.onActivityResult(requestCode, resultCode, data);

    }
}
